package com.qf.lanzhou.blog.api.controller;

import com.qf.lanzhou.blog.api.pojo.entity.Article;
import com.qf.lanzhou.blog.api.service.ArticleService;
import com.qf.lanzhou.blog.api.service.impl.ArticleServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Yeach-chen
 * @ClassName SearchAriticleServlet
 */
@WebServlet("/search.do")
public class SearchArticleServlet extends BaseServletPost {
    private static ArticleService articleService=new ArticleServiceImp();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        String searchArticle = req.getParameter("searchArticle");
        System.out.println("搜索输入字："+searchArticle);
        //模糊搜索文章,返回List
        List<Article> articlesList ;

        try {
            //搜索成功将文章集合存于标准返回对象中
            articlesList = articleService.checkArticle(searchArticle);
            for (Article article:articlesList) {
                article.setArticleProduce(article.getArticleCreatetime());
            }
            successResultWrite(articlesList,resp);
        }catch (RuntimeException e){
            String message = e.getMessage();
            //失败返回提示
            failedResultWrite(message,resp);
        }
    }
}
